package com.jwsoft.manager.core.dao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bifang.engine.assistant.core.exception.AppException;
import com.jwsoft.manager.common.enums.EduStatusTypeEnum;
import com.jwsoft.manager.core.dao.model.EduDataTaskDetail;
import com.jwsoft.manager.core.dao.mapper.EduDataTaskDetailMapper;
import com.jwsoft.manager.core.dao.service.EduDataTaskDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
 * 数据备份任务详情表服务实现类
 *
 * @author zhiyuan
 * @since 2023-09-13
 */
@Service
public class EduDataTaskDetailServiceImpl extends ServiceImpl<EduDataTaskDetailMapper, EduDataTaskDetail> implements EduDataTaskDetailService {

    /**
     * 根据表名和年份校验是否查询历史年度数据
     * @param year
     * @return
     */
    @Override
    public Boolean verifyQueryHistory(Integer year, String handleTable) {
        if (ObjectUtils.isEmpty(year)){
            throw new AppException("查询的年度不能为空");
        }
        QueryWrapper<EduDataTaskDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(EduDataTaskDetail::getYear,year)
                .eq(EduDataTaskDetail::getHandleTable,handleTable);
        EduDataTaskDetail taskDetail = this.getOne(queryWrapper, false);
        if (ObjectUtils.isEmpty(taskDetail)){
            return false;
        }
        //查询迁移是否执行成功
        if (EduStatusTypeEnum.success.getType().equalsIgnoreCase(taskDetail.getMigrationStatus())){
            return true;
        }
        return false;
    }

    @Override
    public Boolean verifyHistoryEncrypt(Integer year, String handleTable) {
        if (ObjectUtils.isEmpty(year)){
            throw new AppException("查询的年度不能为空");
        }
        QueryWrapper<EduDataTaskDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(EduDataTaskDetail::getYear,year)
                .eq(EduDataTaskDetail::getHandleTable,handleTable);
        EduDataTaskDetail taskDetail = this.getOne(queryWrapper, false);
        if (ObjectUtils.isEmpty(taskDetail)){
            return false;
        }
        //查询迁移是否执加密
        if (EduStatusTypeEnum.success.getType().equalsIgnoreCase(taskDetail.getEncryptStatus())){
            return true;
        }
        return false;
    }

    @Override
    public Boolean verifyDestructionByTable(Integer year, String handleTable, String taskId) {
        if (ObjectUtils.isEmpty(year)){
            throw new AppException("查询的年度不能为空");
        }
        QueryWrapper<EduDataTaskDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(EduDataTaskDetail::getYear,year)
                .eq(EduDataTaskDetail::getTaskId,taskId)
                .eq(EduDataTaskDetail::getHandleTable,handleTable);
        EduDataTaskDetail taskDetail = this.getOne(queryWrapper, false);
        if (ObjectUtils.isEmpty(taskDetail)){
            return false;
        }
        if (EduStatusTypeEnum.success.getType().equalsIgnoreCase(taskDetail.getCleanStatus())){
            return true;
        }
        return false;
    }
}
